package com.zenglx.multilanguage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenglx.multilanguage.domain.User;
import com.zenglx.multilanguage.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author bo5.zeng
 * @Descripition： 统一门户定制工作台(Workbench)表控制层
 * @date 2024/1/8 15:15
 */
@Slf4j
@RestController
@RequestMapping("/v1/demo")
public class DemoController {

    @Autowired
    private UserService userService;

    /**
     * 新增用户
     *
     * @return
     */
    @PostMapping
    public ResponseEntity add(@RequestBody User user) {
        userService.save(user);
        return ResponseEntity.ok("成功");
    }

    /**
     * 用户详情
     *
     * @return
     */
    @GetMapping
    public ResponseEntity<User> get(@RequestParam("id") Long id) {
        return ResponseEntity.ok(userService.getById(id));
    }

    /**
     * 查询用户列表
     *
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<User>> list() {
        return ResponseEntity.ok(userService.list());
    }

    /**
     * 更新用户
     *
     * @return
     */
    @PutMapping
    public ResponseEntity update(@RequestBody User user) {
        userService.update(user,null);
        return ResponseEntity.ok("成功");
    }


    /**
     * 删除用户
     *
     * @return
     */
    @DeleteMapping
    public ResponseEntity delete(@RequestParam("id") Long id) {
        userService.removeById(id);
        return ResponseEntity.ok("成功");
    }


    @GetMapping("/page")
    public ResponseEntity<Page<User>> page(@RequestParam(required = false, value = "name") String name,
                                                         @RequestParam(defaultValue = "1", value = "page") Integer page,
                                                         @RequestParam(defaultValue = "10", value = "pageSize") Integer pageSize) {
        Page<User> param = new Page<>(page,pageSize);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.hasText(name),User::getName,name);
        Page<User> userPage = userService.page(param,lambdaQueryWrapper);
        return ResponseEntity.ok(userPage);
    }

}
